package com.hundsun.yr.universal.p2p.datahandler;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.bean.AdSlideBeen;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.impl.StringRequestInteractorImpl;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.hundsun.yr.universal.p2p.ui.activity.ForgetPasswordResetPwdActivity;
import com.hundsun.yr.universal.p2p.utils.AndroidDes3Util;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.GsonTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * 实名认证数据处理基类
 *
 * @author tonglj
 */
public class ForgetPasswordResetPwdHandler extends BaseDataHandler {

    private HSSharedPreferences mConfigSP;

    public ForgetPasswordResetPwdHandler(Context mContext) {
        super(mContext);
        mConfigSP = new HSSharedPreferences(mContext, Config.SPPath.file_configInfo);
    }

    /**
     * 发送验证码
     *
     * @param
     */
    @SuppressWarnings("static-access")
    protected void sendSms(Object... objs) {
        final String phonetext = objs[0].toString();
        // 需不需要跳转
        final boolean flag = objs.length == 1;
        // 发送注册验证码
        StringRequestInteractorImpl requestinteractor = new StringRequestInteractorImpl(new BaseMultiLoadedListener<String>() {
            // TODO: 2017/5/17 返回的数据逻辑有误
            @Override
            public void onSuccess(String task_name, int event_tag, String data) {// 请求成功
                try{
                    AdSlideBeen adSlideBeen = (AdSlideBeen) GsonTool.getInstanceByJson(AdSlideBeen.class, data);
                    if (flag) {
                        //step1点击
                        switch (task_name) {
                            case Constants.REQUESTCODE_000000:
                                //success
                                try {
                                    String decode = AndroidDes3Util.decode(adSlideBeen.data, mConfigSP.getShareProf("config"), (Date.parse(adSlideBeen.timestamp) + "").substring((Date.parse(adSlideBeen.timestamp) + "").length() - 8, (Date.parse(adSlideBeen.timestamp) + "").length()));
                                    String[] split = decode.split("\\n");
                                    Log.e(TAG, "decode: " + decode);
                                    Bundle bundle = new Bundle();
                                    //请求重设验证码成功，将手机号带过去
                                    bundle.putString(Extras.PHONE, phonetext);
                                    //成功获取验证码，准备修改密码
                                    readyGo(ForgetPasswordResetPwdActivity.class, bundle);
                                } catch (Exception e) {
                                    new HSToast(mContext).showToast(mContext.getString(R.string.date_error));
                                    e.printStackTrace();
                                }
                                break;
                            default:
                                break;
                        }
                    } else {

                    }
                }catch (Exception e){
                    new HSToast(mContext).showToast(mContext.getString(R.string.date_error));
                    e.printStackTrace();
                }

                closeProgress();
            }

            @Override
            public void onException(String task_name, String msg) {
                showToast(R.string.check_net);
                if (flag) {
                    closeProgress();
                }
            }
        });

        // TODO: 2017/5/17 收短信
        String zwgt_data = BaseRequestTool2.getFormatedDateString();

        JSONObject map = new JSONObject();
        try {
            //type=2找回密码  phone电话号码  channel渠道0短信
            map.put("type", objs[0]);
            map.put("phone", objs[1]);
            map.put("channel", objs[2]);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JSONObject jsonParams = CommTool.getJSONParams(map, mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data);
        requestinteractor.getRequestData(TaskConstants.TASK_SENDSMS, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SENDSMS,
                Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.
                getString(R.string.api_user_test) +
                        TaskConstants.TASK_SENDSMS, "", mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data));
    }

}
